<!DOCTYPE html>
<uieip type="page" value="login" debug="true" timeout="5000"></uieip>
<uieip type="reg" value="console"></uieip>
<html>

<head>
    <uieip type="file" value="inc" src="/yqwin/easyui/include.txt" cache="true" minute="1440"></uieip>
    <uieip type="file" value="css" src="/yqwin/css/yqwin.min" cache="false"></uieip>
    <uieip type="file" value="js" src="/yqwin/src/yqwin.min" cache="false"></uieip>
    <script src="/yqwin/src/yq.chart.db.rel.js"></script>
    <title>diagram</title>
    <style>
        body {
            margin: 0px;
        }

        table {
            border-spacing: 0px;
        }
    </style>
</head>

<body>
    <div id="diagramDiv">
    </div>
    <script>
        var dbname = null;
        var sid = null;
        var row = {};

        function form_load() {
            dbname = $.urlRequest("dbname");
            sid = $.urlRequest("sid");
            if (sid == "") {
                sid = null;
            }

            console.log("dbname", dbname);
            console.log("sid", sid);

            $(window).resize(function () {
                diagram.onresize();
            });
            diagram.onresize();
            if (sid === null) {
                return;
            }

            var opts = {
                dbname: dbname,
                tbname: "sys_tb_diagram",
                keyid: "sid",
                keyval: sid
            };

            $.yqDbTbRow(opts, function (res) {
                if (res.error == 0) {
                    row = res.row;
                    diagram.setValue();
                } else {
                    alert(res.info);
                }
            });
        }

        $(function () {
            // var p_iframenum = $.urlRequest("p_iframenum");
            // var $frame = $.yqFormMdi_fun("menuwin_get", p_iframenum);
            // console.log($frame);
            // if (!($frame === null)) {
            //     $frame.hello();
            // }
            diagram.init();
        });

        var diagram = {
            id: "diagramDiv",
            init: function () {
                $("#" + diagram.id).yqChartDbRel({
                    isEdit: true,
                    value: {},
                    beforBindingTitle: function (callback) {
                        callback();
                    }, //标题绑定前
                    afterBindingTitle: function () {
                        form_load();
                    },   //标题绑定后
                    selectTable: function (prevent_e) {
                        diagram.selectTable(prevent_e);
                    },
                    onSave: function (val) {  //保存
                        row = $.extend(row, val.info);
                        row["data"] = JSON.stringify(val);
                        var opts = {
                            dbname: "yqbase",
                            tbname: "sys_tb_diagram",
                            rows: [{
                                "action": (sid === null ? "add" : 'upp'),
                                "row": row
                            }]
                        };

                        $.yqDbAction(opts, function (res) {
                            //console.log("save", res);
                            var err = "";
                            if (res.error == 0) {
                                if (res.rows[0].error == 0) {
                                    sid = res.rows[0].sid;
                                    row["sid"] = sid;

                                    var p_iframenum = $.urlRequest("p_iframenum");
                                    var $frame = $.yqFormMdi_fun("menuwin_get", p_iframenum);
                                    if (!($frame === null)) {
                                        if ($frame.diagram.dbname == dbname) {
                                            if (res.rows[0].action == "add") {
                                                $frame.diagram.appendRow(row);
                                            } else {
                                                $frame.diagram.updateRow(row);
                                            }
                                        }
                                    }
                                } else {
                                    err = res.rows[0].info;
                                }
                            } else {
                                err = "error";
                            }

                            if (err != "") {
                                $.yqMessager("error", err);
                            } else {
                                $.yqMessager("success", "执行成功。");
                            }
                        });
                    }
                });
            },
            onresize: function () {
                var w = window.innerWidth;
                var h = window.innerHeight;

                $("#" + diagram.id).yqChartDbRel('resize', {
                    width: w,
                    height: h
                });
            },
            setValue: function () {
                var val = JSON.parse(row["data"]);
                $("#" + diagram.id).yqChartDbRel('setValue', val);
            },
            selectTable: function (prevent_e) {
                var h = window.innerHeight - 100;
                if (h > 600) {
                    h = 600;
                }
                var _pops = {
                    prevent_e: prevent_e,
                    mdiinfo: $.yqGetWindParams(),
                    title: "选择表",
                    width: 350,
                    height: h,
                    buttons: [],
                    closeBtn: false,
                    onOpen: function ($body) {
                        var $this_dialog = this;

                        var $tree_div = $("<div></div>");
                        $body.append($tree_div);
                        $tree_div.yqTree({
                            dbname: dbname,
                            tbname: "sys_tb",
                            keyId: "sid",
                            bindingKeyId: null,
                            bindingValue: null,
                            pKeyId: "pid",
                            pKeyValue: null,
                            loadDataType: 2,
                            displayMember: "describe",
                            loadData: true,
                            menuType: 4,
                            overflowY: true,
                            width: $body.width() - 1,
                            height: $body.height(),
                            isEdit: false,
                            afterSetdata: function (data) {
                                //console.log("afterSetdata", this);
                            }, //绑定数据之后
                            onDblClick: function (node) {
                                if (node == null) {
                                    return;
                                }
                                if (node.attributes.tb_type == 1) {
                                    $.yqDialog_fun.call($this_dialog, "close");
                                    //console.log("node", node.attributes);
                                    diagram.addTable(node.attributes.sid, function (fields) {
                                        var datanode = {
                                            "name": node.attributes.name,
                                            "isdata": true,
                                            "fields": fields
                                        }

                                        //console.log(datanode);
                                        $("#" + diagram.id).yqChartDbRel("addNode", datanode);
                                    });
                                }
                            },
                        });
                    }
                };
                //console.log(_pops);
                if (window.top == window.self) {
                    $.yqDialog(_pops);
                }
                else {
                    window.top.$.yqDialog(_pops);
                }
            },
            addTable: function (tb_id, callback) {
                var opts = {
                    action: "select",
                    dbname: "yqbase",                //数据库
                    tbname: "sys_tb_field",                       //表
                    where: "tb_id='" + tb_id + "'",
                    order: "queue"
                };
                $.yqDbTbSelect(opts, function (res) {
                    var fields = [];
                    if (res.error == 0) {
                        for (var i = 0; i < res.rows.length; i++) {
                            fields.push({
                                "name": res.rows[i].name,
                                "info": res.rows[i].ui_title,
                                //"type": -1
                            });
                        }
                    }
                    console.log(res);

                    callback(fields);
                })
            }
        }
    </script>
</body>

</html>